package com.examination.client.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.examination.client.entity.CourierDeliveryImageInfo;
import com.examination.client.entity.CourierInfo;
import com.examination.client.entity.CourierSite;
import com.examination.client.entity.UserInfo;
import com.examination.client.enums.DeleteStatusEnum;
import com.examination.client.mapper.CourierDeliveryImageMapper;
import com.examination.client.service.ICourierDeliveryImageService;
import com.examination.client.service.ICourierSiteService;
import com.examination.client.util.ExpressOrderNumRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 快递出库图片存储 服务实现类
 * </p>
 *
 * @author tm
 * @since 2022-04-27
 */
@Slf4j
@Service
public class CourierDeliveryImageServiceImpl extends ServiceImpl<CourierDeliveryImageMapper, CourierDeliveryImageInfo> implements ICourierDeliveryImageService {
    @Resource
    private CourierInfoServiceImpl courierInfoService;

    @Resource
    private UserInfoServiceImpl userInfoService;

    @Resource
    ICourierSiteService courierSiteService;


    @Override
    public boolean saveDeliveryImage(String imageUrl, String courierCode, int courierStatus) {
        if (courierCode.contains(",")){
            supplementCourierInfo(courierCode.split(",")[0],imageUrl,courierCode.split(",")[1],courierCode.split(",")[2]);
            courierCode = courierCode.split(",")[0];
        }else{
            supplementCourierInfo(courierCode,imageUrl);
        }
        LambdaQueryWrapper<CourierDeliveryImageInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourierDeliveryImageInfo::getCourierCode, courierCode);
        queryWrapper.eq(CourierDeliveryImageInfo::getDeleteStatus, DeleteStatusEnum.NOT_DELETED.getCode());
        CourierDeliveryImageInfo historyInfo = getOne(queryWrapper);
        if (historyInfo == null) {
            CourierDeliveryImageInfo courierDeliveryImageInfo = new CourierDeliveryImageInfo();
            courierDeliveryImageInfo.setImageUrl(imageUrl);
            courierDeliveryImageInfo.setCourierCode(courierCode);
            courierDeliveryImageInfo.setCourierStatus(courierStatus);
            courierDeliveryImageInfo.setCreateTime(new Date());
            courierDeliveryImageInfo.setUpdateTime(new Date());
            courierDeliveryImageInfo.setDeleteStatus(0);
            return save(courierDeliveryImageInfo);
        } else {
            historyInfo.setUpdateTime(new Date());
            historyInfo.setImageUrl(imageUrl);
            return updateById(historyInfo);
        }
    }

    private void supplementCourierInfo(String courierCode,String imageUrl){
        LambdaQueryWrapper<CourierInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourierInfo::getCourierCode, courierCode);
        queryWrapper.eq(CourierInfo::getDeleteStatus, DeleteStatusEnum.NOT_DELETED.getCode());
        List<CourierInfo> historyInfo = courierInfoService.list(queryWrapper);
        try {
            if(ObjectUtil.isAllEmpty(historyInfo)){
                log.info("SupplementCourierInfo:isNull:{}",courierCode);
                CourierInfo courierInfoDto = new CourierInfo();
                courierInfoDto.setCourierName("未知（入库异常）");
                courierInfoDto.setCourierCode(courierCode);
                courierInfoDto.setCourierStatus(2);
                courierInfoDto.setAdviceStatus(0);
                //courierInfoDto.setImageUrl(imageUrl);
                courierInfoDto.setSiteType(1);
                courierInfoDto.setSiteId(1);
                courierInfoDto.setNextSiteId(55);
                courierInfoDto.setStorageTime(DateUtil.date());
                courierInfoDto.setDeliveryTime(DateUtil.date());
                courierInfoDto.setCreateTime(DateUtil.date());
                courierInfoDto.setUpdateTime(DateUtil.date());
                courierInfoDto.setDeleteStatus(0);
                courierInfoDto.setOperatorId(7);
                courierInfoDto.setUserPhoneHide(0);
                courierInfoService.save(courierInfoDto);

                courierInfoDto.setSiteType(2);
                courierInfoDto.setSiteId(55);
                courierInfoDto.setNextSiteId(null);
                courierInfoService.save(courierInfoDto);

            }else {
                log.info("SupplementCourierInfo:isNotNull:{}",courierCode);
            }
        }catch (Exception e){
            log.info("SupplementCourierInfo:Exception:{}",e.getLocalizedMessage());
        }

    }

    private void supplementCourierInfo(String courierCode,String imageUrl,String userName,String password){
        LambdaQueryWrapper<CourierInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourierInfo::getCourierCode, courierCode);
        queryWrapper.eq(CourierInfo::getDeleteStatus, DeleteStatusEnum.NOT_DELETED.getCode());
        List<CourierInfo> historyInfo = courierInfoService.list(queryWrapper);
        UserInfo userInfo = userInfoService.findByUsernameToPassword(userName, password);
        CourierSite couSite = courierSiteService.getById(userInfo.getSiteId());
        try {
            if(ObjectUtil.isAllEmpty(historyInfo)){
                log.info("SupplementCourierInfo:isNull:{}",courierCode);
                CourierInfo courierInfoDto = new CourierInfo();
                courierInfoDto.setUserName(userInfo.getUserName());
                courierInfoDto.setUserPhone(userInfo.getUserPhone());
                courierInfoDto.setUserAddress(couSite.getSiteAddress());
                courierInfoDto.setCourierName(ExpressOrderNumRule.OrderNumRuleToExpress(courierCode));//"未知（入库异常）"
                courierInfoDto.setCourierCode(courierCode);
                courierInfoDto.setCourierStatus(2);
                courierInfoDto.setAdviceStatus(0);
                courierInfoDto.setImageUrl(imageUrl);
                if (couSite.getParentId() == 0){
                    courierInfoDto.setSiteType(2);
                    courierInfoDto.setSiteId(couSite.getId());
                    courierInfoDto.setNextSiteId(null);
                }else{
                    courierInfoDto.setSiteType(1);
                    courierInfoDto.setSiteId(couSite.getParentId());
                    courierInfoDto.setNextSiteId(couSite.getId());
                }
                courierInfoDto.setStorageTime(DateUtil.date());
                courierInfoDto.setDeliveryTime(DateUtil.date());
                courierInfoDto.setCreateTime(DateUtil.date());
                courierInfoDto.setUpdateTime(DateUtil.date());
                courierInfoDto.setDeleteStatus(0);
                courierInfoDto.setOperatorId(7);
                courierInfoDto.setUserPhoneHide(0);
                courierInfoService.save(courierInfoDto);

                if (couSite.getParentId() != 0){
                    courierInfoDto.setSiteType(2);
                    courierInfoDto.setSiteId(couSite.getId());
                    courierInfoDto.setNextSiteId(null);
                    courierInfoService.save(courierInfoDto);
                }

            }else {
                for (CourierInfo courierInfo: historyInfo) {
                    courierInfo.setCourierStatus(2);
                    courierInfo.setDeliveryTime(DateUtil.date());
                    courierInfo.setUpdateTime(DateUtil.date());
                    courierInfoService.updateById(courierInfo);
                }
                log.info("SupplementCourierInfo:isNotNull:{}",courierCode);
            }
        }catch (Exception e){
            log.info("SupplementCourierInfo:Exception:{}",e.getLocalizedMessage());
        }

    }

}
